import 'package:ble_printer/bean/ble_device.dart';

class EventType {
  static const int BLE_FOUND = 1;
  static const int BLE_CONNECTED = 2;
  static const int BLE_CONNECTE_FAILED = 3;
  static const int BLE_DISCONNECTED = 4;
  static const int BLE_CONNECTE_NODEVICE = 5;
  static const int BLE_ENABLE = 8;
  static const int BLE_DISABLE = 9;
}

class BleRetData<T> {
  int type;
  int ret; //1成功 0 不成功
  T? data;

  BleRetData({required this.type, required this.ret, required this.data});

  factory BleRetData.fromJson(Map<String, dynamic> json, int type) {
    var data;
    var mapData = json['data'];
    if (type == EventType.BLE_FOUND) {
      data = (mapData as List?)
          ?.map((e) =>
              e == null ? null : BleDevice.fromJson(e as Map<String, dynamic>))
          .toList();
    } else if (type == EventType.BLE_CONNECTED) {
      data = mapData ?? '';
    }

    return BleRetData<T>(
      type: json['type'],
      ret: json['ret'],
      data: data,
    );
  }

  Map<String, dynamic> toJson(BleRetData instance) => <String, dynamic>{
        'type': instance.type,
        'ret': instance.ret,
        'data': instance.data,
      };

  @override
  String toString() {
    return 'BleRetData{type: $type, ret: $ret, data: $data }';
  }
}
